<?php

if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
    require 'rb.php';
} else {
    require 'rb_compat.php';
}
require 'formatter.php';
require 'model.php';

class Database extends R {

    public static function config($config) {
        switch ($config['driver']) {
            case 'mysql':
                $dsn = self::getMysqlDsn($config);
                break;
            case 'sqlite':
            default:
                $dsn = self::getSqliteDsn($config);
                break;
        }

        $user = (!empty($config['user'])) ? $config['user'] : null;
        $password = (!empty($config['password'])) ? $config['password'] : null;

        RedBean_ModelHelper::setModelFormatter(new DatabaseFormatter);
        self::setup($dsn, $user, $password);
    }

    private static function getMysqlDsn($config) {
        return sprintf("mysql:host=%s;dbname=%s",$config['host'],$config['dbname']);
    }

    private static function getSqliteDsn($config) {
        return sprintf('sqlite:%s', $config['path']);
    }

}
?>